//index.js
//获取应用实例
const app = getApp();
const getToken = require('../../utils/get_token.js');
let openId = '', memberId = '', gd='';
let cookie, token;
const ckToken = async (that) => {//token确认
  let nTime = Math.floor(new Date().getTime() / 1000);
  let oldTime = wx.getStorageSync('ntime') || 0;
  if (nTime < oldTime)//现在的时间小于到期时间
  {
    hotList(that);
  } else {
    let tokens = await getToken.getToken();
    hotList(that);
  }
}
const hotList = function (that) {//地区限制
  let params = ['shop/pingTuan/getGoodsSaleAreaDetail', {"goodsId": gd }]
  getToken.getData(function (res) {
    let data = res.data;
    console.log(data)
    if (data.areaList.children.length > 0) {
      for (let i = 0; i < data.areaList.children.length; i++)//一级循环，省级
      {
        if (data.areaList.children[i].children.length <= 0) {
          data.areaList.children[i].children.push({ 'name': '所有地区' })
        } else {
          for (let j = 0; j < data.areaList.children[i].children.length; j++)//二级循环，市级j
          {
            if (data.areaList.children[i].children[j].children.length > 0) {
              let area = data.areaList.children[i].children[j].children.reduce(function (prev, cur, index) {
                if (index == 0) {
                  if (index == data.areaList.children[i].children[j].children.length - 1) {
                    return '(' + cur.name + ')';
                  } else {
                    return '(' + cur.name;
                  }

                } else if (index == data.areaList.children[i].children[j].children.length - 1) {
                  return prev + '/' + cur.name + ')';
                } else {
                  return prev + '/' + cur.name;
                }
              }, '');
              data.areaList.children[i].children[j].show = area;
            } else {
              data.areaList.children[i].children[j].show = '';
            }
          }
        }
      }
     }
    that.setData({
      aList: data.areaList.children
    })
  }, ...params)

}
Page({
  data: {
    hidden: false,
    boolT: true,
    aList: []
  },
  onLoad: function (options) {
    let that = this;
    gd=options.id;
    cookie = wx.getStorageSync('cookie') || '';
    token = wx.getStorageSync('token') || '';
    ckToken(that);
    // let data = [{ 'name': '万州市' }, { 'name': '大州市' }, { 'name': '万佛' }, { 'name': '万州市' }]
    // let sum = data.reduce(function (prev, cur,index) {
    //   if(index==0)
    //   {
    //     return '(' + cur.name;
    //   } else if (index == data.length-1)
    //   {
    //     return prev + '/' + cur.name+')';
    //   }else{
    //     return prev + '/' + cur.name;
    //   }
    // },'');
    // console.log(sum)
  },
  onShow: function () {

  },

})
